package club.banyuan.demo.demo6;

import javax.swing.plaf.basic.BasicButtonUI;
import java.io.*;

/**
 * @author edz
 * @version 1.0
 * @date 2020/12/17 下午3:54
 */
public class CopyFoldersDemo {
    public static void main(String[] args) throws IOException {
        File srcFile = new File("/Users/edz/Desktop/2010");
        File destFile = new File("javaSE_Basic");
        copyFolder(srcFile,destFile);
    }

    private static void copyFolder(File srcFile, File destFile) throws IOException {
        if(srcFile.isDirectory()){
            String srcFileName = srcFile.getName();
            File destNewFolder = new File(destFile, srcFileName);
            if(!destNewFolder.exists()){
                destNewFolder.mkdir();
            }
            File[] listFiles = srcFile.listFiles();
            for (File listFile : listFiles) {
                copyFolder(listFile,destNewFolder);
            }
        }else{
            File destNewFile=new File(destFile,srcFile.getName());
            copyFile(srcFile,destNewFile);
        }
    }

    private static void copyFile(File srcFile, File destNewFile) throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destNewFile));
        byte[] bys=new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bos.close();
        bis.close();
    }
}
